package keeto.com.vn.classified.task;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Vector;

import keeto.com.vn.classified.core.ClassifiedCore;
import keeto.com.vn.classified.core.ClassifiedListener;
import keeto.com.vn.classified.network.IProtocol;
import keeto.com.vn.classified.request.PublishRequest;
import keeto.com.vn.classified.request.UploadPictureRequest;
import keeto.com.vn.classified.response.PublishResponse;
import keeto.com.vn.classified.response.UploadPictureResponse;
import keeto.com.vn.classified.ui.MainApplication;
import keeto.com.vn.classified.utils.Base64;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;
import org.json.JSONObject;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

public class TaskPublish extends AbstractTask {


	public TaskPublish(ClassifiedListener listener, ClassifiedCore core,
			JSONObject content, Vector<String> uploadPictures, String userName) {
		super(listener, core);
		this.content = content;
		this.uploadPictures = uploadPictures;
		this.userName = userName;
	}

	JSONObject content;
	Vector<String> uploadPictures;
	String userName;


	@Override
	public void run() throws ClientProtocolException, JSONException,
	IOException {
		Vector<String> pictureNames = new Vector<String>();
		for (String filePath : uploadPictures) {
			int tryTime = 0;
			Log.e("TEST", "Upload picture " + filePath);
			String name = "";
			while (tryTime < 3) {
				name = upload(filePath);
				if (name.equals("")) {
					tryTime++;
					if (tryTime >= 3) {
						callBack(200, name);
						return;
					}
				} else {
					pictureNames.add(name);
					tryTime = 5;
				}
			}
		}
		PublishRequest request = new PublishRequest();
		request.setAuthKey(authKey);
		request.setContent(content);
		request.setImages(pictureNames);
		request.setCreateBy(userName);
		PublishResponse response = json.publish(request);
		for (ClassifiedListener listener : listeners) {
			listener.finishPostPublish(response.getReturnCode(), "");
		}
	}

	private void callBack(int status, String failUpload) {
		for (ClassifiedListener listener : core.copyAndReconstructListeners(null)) {
			listener.finishPostPublish(IProtocol.STATUS_CODE_GENERICFAIL_500,failUpload);
		}
	}

	@Override
	public void timeoutCallback() {
		for (ClassifiedListener l : core.copyAndReconstructListeners(listener)) {
			l.finishPostPublish(IProtocol.STATUS_CODE_CONNECTION_TIMEOUT_502,
					null);
		}

	}

	public String upload(String path) throws ClientProtocolException,
	JSONException, IOException {
		Log.d("TEST", "file = " + path);
		byte[] body = null;
		File imagefile = new File(path);
		FileInputStream fis = new FileInputStream(imagefile);
		Bitmap bitmap = BitmapFactory.decodeStream(fis);
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
		body = outputStream.toByteArray();
		Log.d("Upload file34343", body.toString());
		String content = Base64.encodeToString(body, false);
		Log.d("Upload file", content);
		UploadPictureRequest request = new UploadPictureRequest();
		request.setAuthKey(MainApplication.getAuthenKey());
		request.setContent(content);
		Log.d("Test", request.toString());
		UploadPictureResponse response = json.uploadPicture(request,content);
		Log.d("Test", "Upload return :" + response.getReturnCode());
		if (response.getReturnCode() != IProtocol.STATUS_CODE_OK_200) {
			return "";
		}
		return response.getFileName();
	}
}
